package com.study.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.study.model.pojo.Dept;
import com.study.model.pojo.Emp;
import com.study.model.pojo.Rights;
import com.study.model.pojo.Todo;
import com.study.services.DeptServices;
import com.study.services.EmpServices;
import com.study.services.EmpwithrightServices;
import com.study.services.RightsServices;
import com.study.services.TodoServices;

@RestController
@CrossOrigin
public class ManagerController {
	
	@Autowired
	EmpServices es;
	@Autowired
	RightsServices rs;
	@Autowired
	DeptServices ds;
	@Autowired
	EmpwithrightServices ers;
	@Autowired
	TodoServices tds;
	
	/**
	 * 加载经理管理页面
	 * */
	@RequestMapping("/showmanager")
	public JSONObject showmanager() {
		JSONObject json = new JSONObject();
		JSONArray json1 = new JSONArray();
		List<Dept> list=ds.findAll();
		for(Dept d: list){
            JSONObject jo = new JSONObject();
            jo.put("deptno", d.getDeptno());
            jo.put("dname", d.getDname());
            jo.put("eid", d.getEid());
            json1.add(jo);
        }
		JSONArray json2 = new JSONArray();
		List<Emp> list2=es.findManager();
		for(Emp e:list2){
			JSONObject jo = new JSONObject();
			jo.put("eid", e.getEid());
			jo.put("ename", e.getEname());
			jo.put("pwd", e.getPwd());
			jo.put("sex", e.getSex());
			jo.put("tel", e.getTel());
			jo.put("adss", e.getAdss());
			jo.put("birth", e.getBirth());
			jo.put("job", e.getJob());
			jo.put("deptno", e.getDeptno());
			jo.put("salary", e.getSalary());
			jo.put("leaderid",e.getLeaderid() );
			jo.put("induct_date", e.getInductDate());
			jo.put("depart_date", e.getDepartDate());
			jo.put("state", e.getState());
			jo.put("dname", e.getDeptno()==null?"暂无":(ds.finddnameByid(e.getDeptno())!=null?(ds.finddnameByid(e.getDeptno())):"暂无"));
			List<Todo> dlist=tds.queryTDbymng(e.getEid());
			jo.put("todosnum",dlist==null?0:dlist.size());
			json2.add(jo);			
		}
		json.put("depts",json1);
		json.put("emps",json2);
		System.out.print("json+depts+emps:"+json);
		return json;
	}
	
	
	/**
	 * 修改经理的电话和密码*/
	@RequestMapping("/changeTelandsal")
	public JSONObject changeTelandsal(Emp e) {
		System.out.println("修改员工的电话和密码和地址～");
		Map m =new HashMap();
		m.put("adss", e.getAdss());
		m.put("eid",e.getEid());
		m.put("tel",e.getTel());
		m.put("salary",e.getSalary());
		es.updateTelandSal(m);
		JSONObject json = new JSONObject();
		json.put("ans", "y");
		return json;
	}
	
	/**
	 * 经理调离现在的部门*/
	@RequestMapping("/mngChangedt")
	public JSONObject mngChangedt(int eid,int deptno) {
		System.out.println("经理调离现在的部门");
		ds.delManager(deptno);
		es.removeMngdeptByid(eid);
		JSONObject json = new JSONObject();
		json.put("ans", "y");
		return json;
	}
	/**
	 * 经理降职为普通员工*/
	@RequestMapping("/mngLower")
	public JSONObject mngLower(int eid,int deptno) {
		System.out.println("经理降职为普通员工");
		ers.mngrightlower(eid);
		ds.delManager(deptno);
		es.MngtoStuffByid(eid);;
		JSONObject json = new JSONObject();
		json.put("ans", "y");
		return json;
	}
	
	@RequestMapping("/swempbydt")
	public JSONArray swempbydt(int deptno) {
		System.out.println("进入了showempbydept");
		JSONArray json = new JSONArray();
		List<Emp> list=es.findAllbydept(deptno);
		for(Emp e:list){
			JSONObject jo = new JSONObject();
			jo.put("eid", e.getEid());
			jo.put("ename", e.getEname());
			jo.put("pwd", e.getPwd());
			jo.put("sex", e.getSex());
			jo.put("tel", e.getTel());
			jo.put("adss", e.getAdss());
			jo.put("birth", e.getBirth());
			jo.put("job", e.getJob());
			jo.put("deptno", e.getDeptno());
			jo.put("salary", e.getSalary());
			jo.put("leaderid",e.getLeaderid() );
			jo.put("induct_date", e.getInductDate());
			jo.put("depart_date", e.getDepartDate());
			jo.put("state", e.getState());
			//List<Rights> rlist=rs.rightnamex(e.getEid(), e.getDeptno());
			List<String> rlist=rs.rightnamey(e.getEid(), e.getDeptno());
			JSONArray jsonr = new JSONArray();
			/*for(Rights r:rlist){
				jsonr.add(r.getRname());
			}*/
			for(String r:rlist){
				jsonr.add(r);
			}
			jo.put("rnames", jsonr);
			jo.put("dname", e.getDeptno()==null?"暂无":(ds.finddnameByid(e.getDeptno())!=null?(ds.finddnameByid(e.getDeptno())):"暂无"));
			json.add(jo);			
		}
		System.out.print("emps:"+json);
		return json;
	}
	
}
